package com.lhl.netty.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

/**
 *
 */
public class AppMsg {
    private String k; //客户端的token
    private Integer t; //消息类型
    private String c; //消息内容,一般为json
    private JSONObject contentJSONObject; //content转化成的json对象,仅作为方便解析消息内容用

    public AppMsg() {}

    public AppMsg(String token, int type, String content) {
        this.k = token;
        this.t = type;
        this.c = content;
    }

    public String toJSON(){
        return JSONObject.toJSONString(this);
    }

    public static AppMsg fromJSON(String json){
        AppMsg msg = new AppMsg();
        JSONObject obj = (JSONObject) JSONObject.parse(json);
        if(obj.containsKey("k")){
            msg.setK(obj.getString("k"));
        }
        if(obj.containsKey("t")){
            msg.setT(obj.getInteger("t"));
        }
        if(obj.containsKey("c")){
            msg.setC(obj.getString("c"));
        }
        return msg;
    }

    public String getParam(String key){
        try{
            if(contentJSONObject==null&& StringUtils.isNotEmpty(c)){
                contentJSONObject = JSONObject.parseObject(c);
            }
            return contentJSONObject.getString(key);
        }catch(Exception e){

        }
        return null;
    }

    public Integer getParamInt(String key){
        try{
            if(contentJSONObject==null&&StringUtils.isNotEmpty(c)){
                contentJSONObject = JSONObject.parseObject(c);
            }
            return contentJSONObject.getInteger(key);
        }catch(Exception e){

        }
        return null;
    }

    public Boolean getParamBool(String key){
        try{
            if(contentJSONObject==null&&StringUtils.isNotEmpty(c)){
                contentJSONObject = JSONObject.parseObject(c);
            }
            return contentJSONObject.getBoolean(key);
        }catch(Exception e){

        }
        return null;
    }

    public JSONObject getParamObj(String key){
        try{
            if(contentJSONObject==null&&StringUtils.isNotEmpty(c)){
                contentJSONObject = JSONObject.parseObject(c);
            }
            return contentJSONObject.getJSONObject(key);
        }catch(Exception e){

        }
        return null;
    }

    public JSONArray getParamArray(String key){
        try{
            if(contentJSONObject==null&&StringUtils.isNotEmpty(c)){
                contentJSONObject = JSONObject.parseObject(c);
            }
            return contentJSONObject.getJSONArray(key);
        }catch(Exception e){

        }
        return null;
    }

    public String getK() {
        return k;
    }

    public void setK(String k) {
        this.k = k;
    }

    public Integer getT() {
        return t;
    }

    public void setT(Integer t) {
        this.t = t;
    }

    public String getC() {
        return c;
    }

    public void setC(String c) {
        this.c = c;
    }
}
